{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import display\n",
    "from ipywidgets import widgets\n",
    "import datetime\n",
    "import math\n",
    "import os\n",
    "import csv\n",
    "import numpy as np\n",
    "import statistics\n",
    "from vega import VegaLite\n",
    "\n",
    "# see https://github.com/vega/vega-lite/\n",
    "\n",
    "rootdir = \"d:/temp/performance/new_benchmark2\"\n",
    "\n",
    "width = 600\n",
    "height = 332\n",
    "\n",
    "class Report(object):\n",
    "    def __init__(self, d):\n",
    "        self.__dict__ = d\n",
    "        \n",
    "class Result():\n",
    "    def __init__(self):\n",
    "        self.Name = \"\"\n",
    "        self.Index = 0\n",
    "        self.TimeMean = 0\n",
    "        self.TimeMax = 0\n",
    "        self.TimeMin = 0\n",
    "        self.TimeStdDev = 0\n",
    "        self.TimeSlope = 0\n",
    "        self.MemoryMean = 0\n",
    "        self.MemoryStdDev = 0\n",
    "        self.MemorySlope = 0\n",
    "        self.CpuMean = 0\n",
    "        self.CpuStdDev = 0\n",
    "        self.CpuSlope = 0\n",
    "\n",
    "class Benchmark():\n",
    "    def __init__(self, filename, data):\n",
    "        self.filename = filename\n",
    "        self.data = data\n",
    "        \n",
    "def strip_dict(d):\n",
    "    h = {}\n",
    "    for k in d:\n",
    "        ks = k.replace(\" \",\"\").replace(\"\\ufeff\",\"\")\n",
    "        h[ks] = d[k].strip()\n",
    "    return h\n",
    "\n",
    "def load_report(filename):\n",
    "    result = []\n",
    "    with open(filename, \"r\", encoding='utf8') as f:\n",
    "        dr = csv.DictReader(f);\n",
    "        for row in dr:\n",
    "            row = strip_dict(row)\n",
    "            r = Report(row)\n",
    "            r.TimeMean = float(r.TimeMean)\n",
    "            r.TimeStdDev = float(r.TimeStdDev)\n",
    "            r.TimeMax = r.TimeMean + r.TimeStdDev\n",
    "            r.TimeMin = r.TimeMean - r.TimeStdDev\n",
    "            r.TimeSlope = float(r.TimeSlope)\n",
    "            r.MemoryMean = float(r.MemoryMean)\n",
    "            r.MemoryStdDev = float(r.MemoryStdDev)\n",
    "            r.MemorySlope = float(r.MemorySlope)\n",
    "            r.CpuMean = float(r.CpuMean)\n",
    "            r.CpuStdDev = float(r.CpuStdDev)\n",
    "            r.CpuSlope = float(r.CpuSlope)\n",
    "        \n",
    "            result += [r]\n",
    "    return result\n",
    "                \n",
    "def load_benchmarks(rootdir):\n",
    "    benchmarks = []\n",
    "    for name in os.listdir(rootdir):\n",
    "        dir = os.path.join(rootdir, name)\n",
    "        if name.startswith(\"benchmark_\") and not name.endswith(\".zip\"):\n",
    "            for report in os.listdir(dir):\n",
    "                if report.endswith(\"summary.csv\"):\n",
    "                    filename = os.path.join(rootdir, name, report)\n",
    "                    r = load_report(filename)\n",
    "                    benchmarks += [Benchmark(filename, r)]\n",
    "    return benchmarks\n",
    "\n",
    "benchmarks = load_benchmarks(rootdir)\n",
    "tests = [i.Test for i in benchmarks[0].data]\n",
    "graphs = []\n",
    "\n",
    "for t in tests:\n",
    "    data = []\n",
    "    index = 0\n",
    "    for b in benchmarks:\n",
    "        for row in b.data:\n",
    "            if row.Test == t:\n",
    "                row.Index = index\n",
    "                index += 1\n",
    "                data += [row.__dict__]\n",
    "    graphs += [(t, data)]\n",
    "    \n",
    "def vega_spec(dataset):\n",
    "    global width, height\n",
    "    h = len(dataset)\n",
    "    spec = {\n",
    "        \"$schema\": \"https://vega.github.io/schema/vega-lite/v3.json\",\n",
    "        \"selection\": {\n",
    "          \"grid\": { \"type\": \"interval\", \"bind\": \"scales\" }            \n",
    "        },\n",
    "        \"columns\": 1,\n",
    "        \"concat\": [            \n",
    "        ]\n",
    "    }\n",
    "    for name, data in dataset:\n",
    "        spec[\"concat\"] += [{\n",
    "            \"title\": name,\n",
    "            \"width\": width, \"height\": height,\n",
    "            \"data\": {\"values\": data},\n",
    "            \"layer\":[                \n",
    "                {\n",
    "                    \"mark\": {\n",
    "                        \"type\": \"errorband\",\n",
    "                        \"extent\": \"ci\"\n",
    "                    },\n",
    "                    \"encoding\": {\n",
    "                        \"x\": {\"field\": \"Index\", \"type\": \"quantitative\", \"scale\": {\"padding\": 0, \"zero\": False}},\n",
    "                        \"y\": {\"field\": \"TimeMin\", \"type\": \"quantitative\", \"scale\": {\"padding\": 0, \"zero\": False}},\n",
    "                        \"y2\": {\"field\": \"TimeMax\", \"type\": \"quantitative\", \"scale\": {\"padding\": 0, \"zero\": False}}\n",
    "                    }\n",
    "                },\n",
    "                {\n",
    "                    \"mark\": \"line\",\n",
    "                    \"encoding\": {\n",
    "                        \"x\": {\"field\": \"Index\", \"type\": \"quantitative\", \"scale\": {\"padding\": 0, \"zero\": False}},\n",
    "                        \"y\": {\"field\": \"TimeMean\", \"type\": \"quantitative\", \"scale\": {\"padding\": 0, \"zero\": False}},\n",
    "                    }\n",
    "                }\n",
    "            ]\n",
    "        }]\n",
    "    return spec\n",
    "\n",
    "VegaLite(vega_spec(graphs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
